package com.lengyue.imakiba.show;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lengyue.imakiba.data.Anime;
import com.lengyue.imakiba.data.AnimeInterface;
import com.lengyue.imakiba.data.Type;
import com.lengyue.imakiba.logic.AnimeLogic;
import com.lengyue.imakiba.logic.TypeLogic;

public class Search extends Controller implements AnimeInterface{
	public void index()
	{
		int cvId = getPara("cvId")==null?0:Integer.parseInt(getPara("cvId"));
		String typeCode = getPara("typeCode");
		String search = getPara("search");
		String state = getPara("state");
		//动漫类型
		List<Type> types = TypeLogic.getAllTypesAndSort();
		setAttr("types", types);
		setAttr("cvId", cvId);
		setAttr("typeCode", typeCode);
		setAttr("search", search);
		setAttr("state", state);
		setAttr("coverPath",coverPath);
		render("/WEB-INF/freeMarkerTemplete/search_result.ftl");
	}
	/**
	 * 查询动漫
	 * @param typeCode 动漫类型
	 * @param search 匹配文本
	 * @param state 动漫状态
	 * @return 匹配的动漫
	 */
	public void getSearchResult()
	{
		String typeCode = getPara("typeCode");
		String search = getPara("search");
		String state = getPara("state");
		int cvId = getPara("cvId")==null?0:Integer.parseInt(getPara("cvId"));
		int page = getParaToInt("page");
		int length = getParaToInt("length");
		Page<Anime> animes = null;
		if(typeCode!=null&&!"".equals(typeCode))
		{
			animes = AnimeLogic.getAnimeByTypeCode(page, length, typeCode);
		}
		if(search!=null&&!"".equals(search))
		{
			animes = AnimeLogic.getAnimeBySearch(page, length, search);
		}
		if(cvId!=0)
		{
			animes =  AnimeLogic.getAnimeByCV(page, length, cvId);
		}
		if(state!=null&&!"".equals(state))
		{
			animes =  AnimeLogic.getAnimeByState(page, length, state);
		}
		renderJson(animes.getList());
	}
}
